﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PIBV
{
    
    public class PID_CauseEffect : PID_BaseClass
    {
        public const string TABLE_NAME_CE = "PID_CauseEffect_TABLE_NAME_CE";
                
        //const string SQL_CE = "SELECT C.Name AS cpName, E.Name AS epName, C.CausePID, E.EffectPID, C.AIL, C.SIL, C.Voting, C.Group, CRF.Information, N1.NotesNo, N2.NotesNo "+
        //    "FROM dbo_tblCENotes AS N1 LEFT JOIN (dbo_tblCENotes AS N2 LEFT JOIN ((dbo_atbl_SAS_CRFPrimary AS CRF INNER JOIN dbo_atbl_SAS_EffectPrimary AS E ON E.Key=CRF.EffectKey) INNER JOIN dbo_atbl_SAS_CausePrimary AS C ON C.Key=CRF.EffectKey) ON N2.[Key]=C.NotesID) ON N1.[Key]=E.NotesID; "+
        //		" WHERE (((C.[Name]) Like '{0}' )AND(C.Deleted=0)AND(CRF.Deleted=0)) "+
	    //   	" ORDER BY C.[Name] ";

        const string SQL_CE = "SELECT C.Name as cpName, E.Name as epName, C.CausePID as [CausePID], E.EffectPID as [EffectPID],C.AIL,C.GROUP,C.SIL,C.Voting, CRF.Information as [Information], '' as [Info],'' as [CauseNote], ''as [EffectNote],''  as [CENote] FROM " +
            "(dbo_atbl_SAS_CRFPrimary AS CRF INNER JOIN dbo_atbl_SAS_EffectPrimary AS E  ON E.Key=CRF.EffectKey) INNER JOIN dbo_atbl_SAS_CausePrimary AS C ON C.Key=CRF.CauseKey WHERE C.[Name] Like '{0}' ORDER BY C.Name ;";

        //ctor
        public PID_CauseEffect()
        {
        }

        public DataTable FillData(string filter)
        {
            filter = filter.Replace("*", "%");

            ds = ExecuteQueryInToDataSet(
                                        String.Format(SQL_CE,filter),
                                        ds,
                                        TABLE_NAME_CE,
                                        true
                                        );
            return ds.Tables[TABLE_NAME_CE];
        }

        public CauseEffect[] WrapDataTable(DataTable t)
        { 
            List<CauseEffect> result = new List<CauseEffect>();
            foreach(DataRow r in t.Rows)
            {
                CauseEffect rec = new CauseEffect();

                rec.CauseName = r["cpName"].ToString();
                rec.CausePID = r["CausePID"].ToString();
                //rec.CauseNoteDesc = "xxx";
                //rec.CauseNoteNo = "xxx";

                //rec.EffectNoteDesc = "xxx";

                //rec.InterlockNoteDesc = "xxx";
                //rec.InterlockNoteNo = "xxx";
                //rec.InterlockDesc = "xxx";

                rec.Voting = r["Voting"].ToString();
                rec.SIL = r["SIL"].ToString();
                rec.AIL = r["AIL"].ToString();
                rec.Interlock = r["Information"].ToString();
                //rec.InterlockDesc = "xxx";

                //rec.EffectNoteNo = "xxx";
                //rec.EffectNoteDesc = "xxx";
                rec.EffectPID = r["EffectPID"].ToString();
                rec.EffectName = r["epName"].ToString();

                rec.Group = r["Group"].ToString();
                
                
                
                //<DataGridTextColumn Header="CauseName" 
                //                           Binding="{Binding CauseName}" />

                //<DataGridTextColumn Header="CausePID" Width="SizeToCells" MinWidth="70"   
                //                           Binding="{Binding CausePID}" />

                //<DataGridTextColumn Header="C.Note" Width="SizeToCells"   MinWidth="15"   
                //                           Binding="{Binding CauseNoteDesc}" />

                //<DataGridTextColumn Header="Voting"  Width="SizeToCells" 
                //                           Binding="{Binding Voting}" />

                //<DataGridTextColumn Header="SIL"  Width="SizeToCells"  MinWidth="35"   
                //                           Binding="{Binding SIL}" />

                //<DataGridTextColumn Header="AIL"  Width="SizeToCells"  MinWidth="35" 
                //                           Binding="{Binding AIL}" />

                //<DataGridTextColumn Header="Interlock"  Width="SizeToCells" MinWidth="65"   
                //                           Binding="{Binding Interlock}" />

                //<DataGridTextColumn Header="I.Desc"  Width="SizeToCells" MinWidth="65"   
                //                           Binding="{Binding InterlockDesc}" />
                
                //<DataGridTextColumn Header="E.Note" Width="SizeToCells" MinWidth="15"   
                //                           Binding="{Binding EffectNoteDesc}" />

                //<DataGridTextColumn Header="EffectPID"  Width="SizeToCells"  MinWidth="70"   
                //                           Binding="{Binding EffectPID}" />

                //<DataGridTextColumn Header="EffectName"  
                //                           Binding="{Binding EffectName}" />

                result.Add(rec);
            }

            return result.ToArray();
        }


    }

    public class CauseEffect
    {

        private string _CauseName;

        public string CauseName
        {
            get { return _CauseName; }
            set { _CauseName = value; }
        }

        private string _EffectName;

        public string EffectName
        {
            get { return _EffectName; }
            set { _EffectName = value; }
        }

        private string _CauseNoteNo;

        public string CauseNoteNo
        {
            get { return _CauseNoteNo; }
            set { _CauseNoteNo = value; }
        }

        private string _CauseNoteDesc;

        public string CauseNoteDesc
        {
            get { return _CauseNoteDesc; }
            set { _CauseNoteDesc = value; }
        }

        private string _EffectNoteNo;

        public string EffectNoteNo
        {
            get { return _EffectNoteNo; }
            set { _EffectNoteNo = value; }
        }

        private string _EffectNoteDesc;

        public string EffectNoteDesc
        {
            get { return _EffectNoteDesc; }
            set { _EffectNoteDesc = value; }
        }

        private string _InterlockNoteNo;

        public string InterlockNoteNo
        {
            get { return _InterlockNoteNo; }
            set { _InterlockNoteNo = value; }
        }

        private string _InterlockNoteDesc;

        public string InterlockNoteDesc
        {
            get { return _InterlockNoteDesc; }
            set { _InterlockNoteDesc = value; }
        }

        private string _Voting;

        public string Voting
        {
            get { return _Voting; }
            set { _Voting = value; }
        }

        private string _Group;

        public string Group
        {
            get { return _Group; }
            set { _Group = value; }
        }

        private string _AIL;

        public string AIL
        {
            get { return _AIL; }
            set { _AIL = value; }
        }

        private string _SIL;

        public string SIL
        {
            get { return _SIL; }
            set { _SIL = value; }
        }

        private string _Interlock;

        public string Interlock
        {
            get { return _Interlock; }
            set { _Interlock = value; }
        }

        private string _InterlockDesc;

        public string InterlockDesc
        {
            get { return _InterlockDesc; }
            set { _InterlockDesc = value; }
        }

        private string _CausePID;

        public string CausePID
        {
            get { return _CausePID; }
            set { _CausePID = value; }
        }

        private string _EffectPID;

        public string EffectPID
        {
            get { return _EffectPID; }
            set { _EffectPID = value; }
        }

        public CauseEffect(){}
    }


}
